<? 
session_start();
include('./database.inc');

if(isset($_SESSION['uid'])){
	header( 'Location: ./slides.php' ) ;
} else {
	if(isset($_POST['submitted'])){
		$user = mysql_real_escape_string($_POST['username']);
		$password = md5($_POST['password']);
		$query = "SELECT * FROM users WHERE username = '$user' AND password = '$password'";
		$result = mysql_query($query);
		$num_rows = mysql_num_rows($result);
		
		if ($num_rows > 0){
			$row = mysql_fetch_array( $result );
			$_SESSION['uid'] = $row['user_id'];
			$_SESSION['superuser'] = $row['superuser'];
			$_SESSION['notification']['type'] = "success";
			$_SESSION['notification']['msg'] = "Successfully Logged In";
			header( 'Location: ./slides.php' ) ;
			die();
		} else {
			?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
			<title>Virtual Microscope Login</title>
			<link rel="stylesheet" type="text/css" href="style.css" />
			</head>

			<body>

			<div id="logincontainer">
				<div id="loginerror">
					Error: Invalid Login Credentials
				</div>
				<div id="logincontent">
					<h1>Login</h1>
					<form method="post" action="index.php" name="login">
						<table style="margin:0 auto">
							<tr style="height:30px;"><td>Username:</td><td><input type="text" name="username" style="width:200px;" /></td></tr>
							<tr style="height:30px;"><td>Password:</td><td><input type="password" name="password" style="width:200px;"/></td></tr>
							<tr style="height:30px;"><td></td><td style="text-align:right"><input type="submit" value="Login" name="submit" /></td></tr>
						</table>
						
						<input type="hidden" name="submitted" value="submitted" />
					</form>

				</div>
			</div>

			</body>

			</html>


			<?
		}

		
	} else {
		?>
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
			<html xmlns="http://www.w3.org/1999/xhtml">
			<head>
			<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
			<title>Virtual Microscope Login</title>
			<link rel="stylesheet" type="text/css" href="style.css" />
			</head>

			<body>

			<div id="logincontainer">
				<? if (isset($_GET['logged'])){?>
				<div id="loginerror" class="success">
					Successfully Logged Out
				</div>
				<?
				}
				?>
				<div id="logincontent">
					<h1>Login</h1>
					<form method="post" action="index.php" name="login">
						<table style="margin:0 auto">
							<tr style="height:30px;"><td>Username:</td><td><input type="text" name="username" style="width:200px;" /></td></tr>
							<tr style="height:30px;"><td>Password:</td><td><input type="password" name="password" style="width:200px;"/></td></tr>
							<tr style="height:30px;"><td></td><td style="text-align:right"><input type="submit" value="Login" name="submit" /></td></tr>
						</table>
						
						<input type="hidden" name="submitted" value="submitted" />
					</form>

				</div>
			</div>

			</body>

			</html>
		<?
	}
}
?>

